using Microsoft.AspNetCore.Mvc;
using Lzfy_His_Service.Services;
using Lzfy_His_Service.Models;
using Microsoft.AspNetCore.Authorization;

namespace Service.Controllers
{
    /// <summary>
    /// 退费申请控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class RefundController : ControllerBase
    {
        private readonly IRefundService _refundService;
        private readonly IHisPatientBalanceService _hisPatientBalanceService;
        private readonly ILogger<RefundController> _logger;

        public RefundController(
            IRefundService refundService,
            IHisPatientBalanceService hisPatientBalanceService,
            ILogger<RefundController> logger)
        {
            _refundService = refundService;
            _hisPatientBalanceService = hisPatientBalanceService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有退费申请
        /// </summary>
        /// <returns>退费申请列表</returns>
        [HttpPost("get-all")]
        public async Task<ActionResult<IEnumerable<RefundResponse>>> GetAllRefunds()
        {
            var refunds = await _refundService.GetAllRefundsAsync();
            return Ok(refunds);
        }



        /// <summary>
        /// 获取患者HIS系统余额
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <returns>患者余额</returns>
        [HttpGet("patient-balance/{patientId}")]
        public async Task<ActionResult> GetPatientBalance(string patientId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(patientId))
                {
                    return BadRequest(new { message = "患者ID不能为空" });
                }

                var balance = await _hisPatientBalanceService.GetPatientBalanceAsync(patientId);
                
                return Ok(new { 
                    success = true, 
                    data = new { 
                        patientId = patientId,
                        balance = balance ?? 0
                    },
                    message = "获取患者余额成功" 
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { 
                    success = false, 
                    message = "获取患者余额失败", 
                    error = ex.Message 
                });
            }
        }

        /// <summary>
        /// 查询患者最近30天的充值记录数量
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <returns>充值记录数量</returns>
        [HttpPost("patient-recharge-count/{patientId}")]
        public async Task<ActionResult> GetPatientRechargeCount(string patientId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(patientId))
                {
                    return BadRequest(new { message = "患者ID不能为空" });
                }

                var count = await _hisPatientBalanceService.GetPatientRechargeCountAsync(patientId);
                
                return Ok(new { 
                    success = true, 
                    data = new { 
                        patientId = patientId,
                        rechargeCount = count,
                        queryRange = "最近30天"
                    },
                    message = "查询患者充值记录数量成功" 
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { 
                    success = false, 
                    message = "查询患者充值记录数量失败", 
                    error = ex.Message 
                });
            }
        }

    }
}